package kr.co.sdsway2.web.client;

import java.io.IOException;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import kr.co.sdsway2.common.AbstractContraller;
import kr.co.sdsway2.service.AvatarService;
import kr.co.sdsway2.service.PtService;
import kr.co.sdsway2.vo.AvatarVO;
import kr.co.sdsway2.vo.PtInfVO;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.codehaus.jackson.JsonProcessingException;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class LoginController extends AbstractContraller {
protected final Log logger = LogFactory.getLog(getClass());

	@Resource(name="avatarService")
	AvatarService avatarService;
	
	@Resource(name="ptService")
	private PtService ptService;
	
	private final String MASTER_KEY = "1331";
	private final String PASSWORD = "1541";
	private final int GUEST_LIMIT_COUNT = 15;
	
	@RequestMapping("/loginPage")
	public ModelAndView handleRequest(HttpServletRequest req, HttpServletResponse res) throws Exception {
		return new ModelAndView("login.jsp");
	}
	
	@RequestMapping("/login")
	public ModelAndView login(HttpServletRequest req, HttpServletResponse res) throws Exception {

		ModelAndView mav = new ModelAndView("remocon.jsp");//view
		
		//param set
		String avatarId = req.getParameter("avatarId");
		String avatarType = req.getParameter("checkedAvatarType");

		if( avatarId != null ) {
			AvatarVO avatarVO = avatarService.getAvatar(avatarId);	
			if(avatarVO==null){
				avatarService.insertAvatar(avatarId, avatarType);	//insert avatar
				avatarVO = avatarService.getAvatar(avatarId);		//get avatar info
			}
			mav.addObject("avatarVO", avatarVO);

			//"점수 (순위/총원)" 출력에 필요한 데이터 수집
			List<AvatarVO> tempAvatarVOList = avatarService.getList();
			AvatarVO tempAvatarVO = null;
			int rank=-1, total=0;
			if( tempAvatarVOList != null ) total = tempAvatarVOList.size();
			for(int i=0; i<total; i++) {
				tempAvatarVO = tempAvatarVOList.get(i);
				if( tempAvatarVO!=null && avatarId.equals(tempAvatarVO.getAvatarId())) {
					rank = i+1;
					break;
				}
			}
			mav.addObject("rank", rank);
			mav.addObject("total", total);
		}
		return mav;
	}
	
	@RequestMapping("/index")
	public ModelAndView index(HttpServletRequest req, HttpServletResponse res) throws Exception {
		
		List<PtInfVO> ptInfVOList = ptService.getAllPtInf(null);
		ModelAndView mav = new ModelAndView("index.jsp");//view
		mav.addObject("ptInfVOList",ptInfVOList);
		return mav;
	}
	
	@RequestMapping("/index2")
	public ModelAndView index2(HttpServletRequest req, HttpServletResponse res) throws Exception {
		
		ModelAndView mav = new ModelAndView("index2.jsp");//view
		String ptId = req.getParameter("ptId");
		String ptName = req.getParameter("ptName");
		req.getSession().setAttribute("ptId", ptId);
		req.getSession().setAttribute("ptName", ptName);
		req.setAttribute("ptId", ptId);
		req.setAttribute("ptName", ptName);
		return mav;
	}
	
	@RequestMapping("/createPt")
	public ModelAndView createPt(HttpServletRequest req, HttpServletResponse res) throws Exception {
		
		ModelAndView mav = new ModelAndView("createPt.jsp");//view
		String ptId = req.getParameter("ptId");
		String ptName = req.getParameter("ptName");
		req.getSession().setAttribute("ptId", ptId);
		req.getSession().setAttribute("ptName", ptName);
		req.setAttribute("ptId", ptId);
		req.setAttribute("ptName", ptName);
		return mav;
	}
	
	@RequestMapping("checkPwdAjax")
	public void checkPwdAjax(Model model, HttpServletRequest req) throws JsonProcessingException, IOException {		
		String reqPwd = req.getParameter("remotepassword");
		if (MASTER_KEY.equals(reqPwd)) {
			model.addAttribute("resultMessage", "SUCC");
		} else if (PASSWORD.equals(reqPwd)) {
			List<AvatarVO> avatarList = avatarService.getList();
			if (avatarList.size() >= GUEST_LIMIT_COUNT) {
				model.addAttribute("resultMessage", "OVER");
			} else {
				model.addAttribute("resultMessage", "SUCC");
			}
		} else {
			model.addAttribute("resultMessage", "FAIL");
		}
	}

	/** [TEST] 부하테스트를 위한 사용자 생성용!! */
	@RequestMapping("/perf1")
	public ModelAndView perf1(HttpServletRequest req, HttpServletResponse res) throws Exception {
		String code = req.getParameter("code");
		if( !"test".equals(code) ) return null;
		ModelAndView mav = new ModelAndView("index.jsp");
		int count = Integer.parseInt(req.getParameter("count"));
		String avatarId = "TEST";
		debug("[TEST] start");
		for(int i=1; i<=count; i++) avatarService.insertAvatar(avatarId+i, "10");
		debug("[TEST] end - size : " + avatarService.getList().size());
		return mav;
	}

}
